package com.tarena.lbs.marketing.web.repository;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tarena.lbs.marketing.web.mapper.CouponCodeMapper;
import com.tarena.lbs.pojo.marketing.po.CouponCodePO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class CouponCodeRepository extends ServiceImpl<CouponCodeMapper, CouponCodePO> {
    @Autowired
    private CouponCodeMapper couponCodeMapper;

    public CouponCodePO getAvailableCode(Integer couponId) {
        //根据优惠券id 查询可用的券码 status=0未使用的第一个元素
        QueryWrapper<CouponCodePO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("coupon_id", couponId);
        queryWrapper.eq("status", 0);
        return couponCodeMapper.selectList(queryWrapper).get(0);
    }

    public void updateCouponCodeStatus(Integer id, Integer status) {
        //update coupon_code set status=1 where id=#{id}
        CouponCodePO poParam=new CouponCodePO();
        poParam.setId(id);
        poParam.setStatus(status);
        couponCodeMapper.updateById(poParam);
    }
}
